package com.gensdai.leliang.db;

import android.content.ContentValues;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.util.Log;

import com.tencent.wcdb.Cursor;
import com.tencent.wcdb.database.SQLiteDatabase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zsxx on 2017/8/1.
 */

public class SearchDao {

    public static final String TABLE_NAME = "SearchHistory";

    public Context mContext;

    public SqlDBHelp dbHelp;

    SQLiteDatabase db;

    public SearchDao(Context mContext) {
        this.mContext = mContext;
        dbHelp = new SqlDBHelp(mContext);
    }



    public boolean addSearchStr(String str){
        boolean flag = true;
        db =  dbHelp.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("SearchStr",str);
        values.put("CountIn",0);
        values.put("timestamp",new Date().toString());
        if(db!= null){
            Cursor cursor = db.query(TABLE_NAME, new String[]{"*"}, "SearchStr = ?", new String[]{str}, null, null, null);
//            Cursor c = db.rawQuery("select * from +"+TABLE_NAME+" where SearchStr=" + str,
            Log.d("addSearchStr" ,"" +cursor.getCount());

            if (cursor != null) {
                cursor.moveToFirst();
                if(cursor.getCount() >0){
                    cursor.getColumnCount();
                    String SearchStr = cursor.getString(cursor.getColumnIndex("SearchStr"));
                    int count = cursor.getInt(cursor.getColumnIndex("CountIn"));

                    ContentValues args = new ContentValues();
                    args.put("CountIn", count+1);
                    args.put("timestamp", new Date().toString());
                    int i = db.update(TABLE_NAME,args,"SearchStr=?",new String[]{SearchStr});
                    Log.d("addSearchStr update",i+"");

                    if (cursor != null) {
                        cursor.close();
                    }
                }else{
                    long i = db.insert(TABLE_NAME,null,values);
                    Log.d("addSearchStr insert",i+"");
                }

            }else{

                long i = db.insert(TABLE_NAME,null,values);
                Log.d("addSearchStr insert",i+"");
            }
        }
        db.close();
        return flag;
    }


    public List<String> findList(){
        List<String> lists = new ArrayList<>();
        db =  dbHelp.getReadableDatabase();
//        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ORDER_BY);
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " order by timestamp desc limit 0,10",null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("SearchStr"));
            Log.d("findList",cursor.getString(cursor.getColumnIndex("timestamp")));
            lists.add(name);
        }
        if (cursor != null) {
            cursor.close();
        }
        if (db != null) {
            db.close();
        }
        return lists;
    }

    public void deletAll(){
        db =  dbHelp.getWritableDatabase();
        int count = db.delete(TABLE_NAME,null,null);
        if (db != null) {
            db.close();
        }
    }
}
